package edu.zqxy.eastmoney.service.impl;

import edu.zqxy.eastmoney.mapper.UserMapper;
import edu.zqxy.eastmoney.pojo.User;
import edu.zqxy.eastmoney.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional//所有方法都需要事务
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;//....
    @Override
    public void balance2Users(User user1, User user2, float amount) {
        System.out.println(userMapper);
        user1.setSalary(user1.getSalary()-amount);
        user2.setSalary(user2.getSalary()+amount);
        userMapper.updateByPrimaryKey(user1);
        //制造异常，制造是否能够回滚
//     int ii=100/0;
        userMapper.updateByPrimaryKey(user2);
    }

    @Override
    public int  save(User user) {
       return  userMapper.insert(user);
    }

    @Override
    public int update(User user) {
    return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int  delete(int id) {
return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User findById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }
}